package com.maixin.code.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.maixin.code.base.BaseAction;
import com.maixin.code.entity.Menus;
import com.maixin.code.entity.Users;
import com.maixin.code.service.MenusService;
import com.maixin.code.service.UsersService;
import com.maixin.code.utils.MenusUtils;
import com.maixin.code.utils.PageUtils;

public class UserLoginAction extends BaseAction{
	
	private static final long serialVersionUID = 1L;
	
	private Users userLogin;
	
	private UsersService usersSer;
	
	private MenusService menusSer;
	
	private PageUtils pageUtils;
	private boolean success;
	private String msg;
	
	private List list;
	
	private String root;
	
	private String jsonStr;
	
	public void setSuccess(boolean success) {
		this.success = success;
	}
	/**
	 * 用户登录请求的Action 
	 * 此处除判断登录是否成功外，还需要通过用户获得其对应的权限以及对于权限所拥有的菜单列表，
	 * 并返回到main.jsp页面新组成菜单界面
	 * @return 成功，直接进入Main.jsp界面，否则重新返回登录界面
	 * @throws Exception
	 */
	public String login()throws Exception{
		List<Users> list = usersSer.findByProperties(new String []{"loginname","password"},  new String[]{userLogin.getLoginname(),userLogin.getPassword()});
		if(null == list||list.size()==0){
			success = false;
			msg = "用户名或密码输入错误!";
			return INPUT;
		}else{
			//保存当前的用户到Session中，方便以后处理
			HttpServletRequest request = ServletActionContext.getRequest();
			request.getSession().setAttribute("currUser", list.get(0));
			success = true;
			root =  buildMenuTree();
			return SUCCESS;
		}
	}
	
	/**
	 * 用户登录进去后查询Menu
	 */
	private String buildMenuTree(){
		List<Menus> menuList = menusSer.getMenusDAO().findAllMenus();
		return MenusUtils.MenusList2Str(menuList);
	}
	
	
	public String mainPanel(){
		System.err.println("HERE IS THE CHINA MAN!");
		return SUCCESS;
	}
	

	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public boolean isSuccess() {
		return success;
	}
	public String getJsonStr() {
		return jsonStr;
	}
	public void setJsonStr(String jsonStr) {
		this.jsonStr = jsonStr;
	}
	public List getList() {
		return list;
	}
	public void setList(List list) {
		this.list = list;
	}
	public Users getUserLogin() {
		return userLogin;
	}
	public void setUserLogin(Users userLogin) {
		this.userLogin = userLogin;
	}
	public UsersService getUsersSer() {
		return usersSer;
	}
	public void setUsersSer(UsersService usersSer) {
		this.usersSer = usersSer;
	}
	public String getRoot() {
		return root;
	}
	public void setRoot(String root) {
		this.root = root;
	}
	public MenusService getMenusSer() {
		return menusSer;
	}
	public void setMenusSer(MenusService menusSer) {
		this.menusSer = menusSer;
	}

}
